import { createI18n } from "vue-i18n";
import elementEnLocale from "element-plus/dist/locale/en.mjs";
import elementZhLocale from "element-plus/dist/locale/zh-cn.mjs";
import adminEnLocale from "./en_US/admin-en.json"; // 导入项目中用到的英文语言包
import adminZhLocale from "./zh_CN/admin-zh.json"; // 导入项目中用到的中文语言包
// import monitorEnLocale from "./en_US/monitor-en.json"; // 导入项目中用到的英文语言包
// import monitorZhLocale from "./zh_CN/monitor-zh.json"; // 导入项目中用到的中文语言包

const convertFlatJsonString = (json: any) => {
  const replaceAll = (input: any, needle: any, replacement: any): string => {
    function replaceString(input: any, needle: any, replacement: any) {
      if (typeof input !== "string") {
        throw new TypeError(
          "Expected input to be a string, got ${typeof input}"
        );
      }

      if (
        !(typeof needle === "string" && needle.length > 0) ||
        !(typeof replacement === "string" || typeof replacement === "function")
      ) {
        return input;
      }

      let ret = "";
      let matchCount = 0;
      let prevIndex = 0;

      if (prevIndex > input.length) {
        return input;
      }

      const t = true;
      while (t) {
        // eslint-disable-line no-constant-condition
        //const index = input.indexOf(needle, prevIndex);
        const index = input.indexOf(needle, prevIndex);

        if (index === -1) {
          break;
        }

        matchCount++;

        //const replaceStr = typeof replacement === 'string' ? replacement : replacement(needle, matchCount, input);
        const replaceStr =
          typeof replacement === "string"
            ? replacement
            : replacement(needle, matchCount, input);

        // Get the initial part of the string on the first iteration
        //const beginSlice = matchCount === 1 ? 0 : prevIndex;
        const beginSlice = matchCount === 1 ? 0 : prevIndex;

        ret += input.slice(beginSlice, index) + replaceStr;

        prevIndex = index + needle.length;
      }

      if (matchCount === 0) {
        return input;
      }

      return ret + input.slice(prevIndex);
    }

    return replaceString(
      input.toString(),
      needle.toString(),
      replacement.toString()
    );
  };

  let jsonFlatArray = "";

  for (let key in json) {
    let child = json[key as any];
    jsonFlatArray += '"' + key + '"' + ":{\n";
    for (let subkey in child) {
      // let fullkey = '"' + key + "." + subkey + '"';
      let fullkey = '"' + subkey + '"';
      let value = '"' + child[subkey as any] + '"';
      value = replaceAll(value, "\n", "\\n");
      jsonFlatArray += fullkey + ":" + value + ",\n";
    }
    if (jsonFlatArray.length > 0) {
      jsonFlatArray = jsonFlatArray.substring(0, jsonFlatArray.length - 2);
    }
    jsonFlatArray += "},\n";
  }
  if (jsonFlatArray.length > 0) {
    jsonFlatArray = jsonFlatArray.substring(0, jsonFlatArray.length - 2);
  }
  console.log("{" + jsonFlatArray + "}");
};

// const elementEnLocaleJson = convertFlatJsonString(elementEnLocale) as any;
// const elementZhLocaleJson = convertFlatJsonString(elementZhLocale) as any;
// console.log("elementEnLocaleJson", elementEnLocaleJson);

// 语言文件格式选择，项目中使用 json 文件
// 语言文件可以是 ts 的，vue-i18n 可以支持
// 如果 vscode 使用了插件 i18n Ally，则语言文件必须是 json 文件，不能是 ts 的
// i18n Ally 配置
// .vscode/settings.json
// {
//   "i18n-ally.localesPaths": ["src/i18n"],
//   // 翻译后变量格式 nested：嵌套式  flat：扁平式
//   "i18n-ally.keystyle": "nested",
//   "i18n-ally.namespace": false,
//   "i18n-ally.enabledParsers": ["json"],
//   // 翻译源语言
//   "i18n-ally.sourceLanguage": "zh_CN",
//   // 显示语言
//   "i18n-ally.displayLanguage": "zh_CN"
// }

const messages = {
  en_US: {
    ...adminEnLocale,
    // ...monitorEnLocale,
    ...elementEnLocale,
    // ...elementEnLocaleJson,
  },
  zh_CN: {
    ...adminZhLocale,
    // ...monitorZhLocale,
    ...elementZhLocale,
    // ...elementZhLocaleJson,
  },
};

// console.log("i18n.messages", messages);

const i18n = createI18n({
  legacy: false, // 使用 Composition API 模式
  // locale: "zh_CN", // 设置语种
  // fallbackLocale: "en_US",
  locale: "zh_CN", // 设置语种
  fallbackLocale: "en",
  globalInjection: true,
  messages, // 设置全局当地语言包,
  numberFormats: {
    //设置 数字本地化
    en_US: {
      currency: {
        //添加 $
        style: "currency",
        currency: "USD",
      },
    },
    zh_CN: {
      currency: {
        //添加 ￥
        style: "currency",
        currency: "JPY",
        currencyDisplay: "symbol",
      },
    },
  },
  dateTimeFormats: {
    //设置 日期时间本地化
    en_US: {
      short: {
        year: "numeric",
        month: "short",
        day: "numeric",
      },
      long: {
        year: "numeric",
        month: "short",
        day: "numeric",
        weekday: "short",
        hour: "numeric",
        minute: "numeric",
      },
    },
    zh_CN: {
      short: {
        year: "numeric",
        month: "short",
        day: "numeric",
      },
      long: {
        year: "numeric",
        month: "short",
        day: "numeric",
        weekday: "short",
        hour: "numeric",
        minute: "numeric",
      },
    },
  },
});

export default i18n;
